package com.hicc.cloud.teacher.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hicc.cloud.R;
import com.hicc.cloud.teacher.activity.FacultyComparedActivity;
import com.hicc.cloud.teacher.activity.ScanActivity;
import com.hicc.cloud.teacher.activity.ScanResultActivity;
import com.hicc.cloud.teacher.utils.ToastUtli;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import mehdi.sakout.fancybuttons.FancyButton;


/**
 * Created by Administrator on 2016/9/24/024.
 * 学院
 */

public class CollegeHomeFragment extends BaseFragment implements View.OnClickListener {
    private static final int SCAN_CODE = 0;

    // 加载数据
    @Override
    public void fetchData() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_faculty, container, false);

        initUI(view);

        return view;
    }

    private void initUI(View view) {
        FancyButton btn_comparison = (FancyButton) view.findViewById(R.id.btn_comparison);
        FancyButton btn_scan = (FancyButton) view.findViewById(R.id.btn_scan);
        btn_scan.setOnClickListener(this);
        btn_comparison.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 报到对比
            case R.id.btn_comparison:
                startActivity(new Intent(getContext(), FacultyComparedActivity.class));
                break;
            // 扫码报到
            case R.id.btn_scan:
                startActivityForResult(new Intent(getContext(), ScanActivity.class), SCAN_CODE);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**
         * 处理二维码扫描结果
         */
        switch (requestCode) {
            case SCAN_CODE:
                //处理扫描结果（在界面上显示）
                if (null != data) {
                    Bundle bundle = data.getExtras();
                    if (bundle == null) {
                        return;
                    }
                    if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                        String result = bundle.getString(CodeUtils.RESULT_STRING);
                        // 解析后操作
                        Intent intent = new Intent(getContext(), ScanResultActivity.class);
                        intent.putExtra("result", result);
                        startActivityForResult(intent, 2);
                    } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                        ToastUtli.show(getContext(), "解析二维码失败");
                    }
                }
                break;
            case 2:
                startActivityForResult(new Intent(getContext(), ScanActivity.class), SCAN_CODE);
                break;
        }
    }
}
